//
//  MeetViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/23.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "MeetViewController.h"
#import "AllPersonJoinMeetTableViewController.h"

@interface MeetViewController ()<UIWebViewDelegate>
{
    UILabel *title;
    LeftTitleTextField *ReleasePeople;  //发布人
    LeftTitleTextField *ReleaseTime;    //发布日期
    LeftTitleTextField *meetPlace;      //会议地点
    LeftTitleTextField *meetHost;       //会议主持人
    LeftTitleTextField *meetTime;       //会议时间
    LeftTitleTextField *ListPersonNames;    //参会人员列表
    
    UITextView *meetMainTopics;     //会议内容
    UILabel *meetMinutesLabel;
    
    
    NSDictionary *meetData; //会议详细信息
    
    UIView *under2;
    UILabel *labelcontent;  //会议纪要
    UIWebView *webViewzqx;  //会议纪要
}
@end

@implementation MeetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会议管理";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];

    
}
- (void)setValue{
    title.text = meetData[@"mTitle"];
    ReleasePeople.text = meetData[@"mCreateRealName"];
    ReleaseTime.text = meetData[@"cTime"];
    meetPlace.text = meetData[@"Address"];
    meetHost.text = @"管理员";
    meetTime.text = meetData[@"mStartTime"];
    meetMainTopics.text = meetData[@"mMainTopics"]; //会议内容
    ListPersonNames.text = meetData[@"ListPersonNames"];   //参会人员列表
    [webViewzqx loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:meetData[@"mContent"]]]]; //会议纪要
    if (![meetData[@"IsWriteOK"] isEqualToString:@"2"]) {
        [webViewzqx mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [under2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [labelcontent mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}
- (void)getData{
    NSString *ashx = @"/Server/MeetingHandler.ashx";
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中" animated:YES];
    NSDictionary *dic = @{@"act":@"getmdetail",@"mid":self.dic[@"ID"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            [GMDCircleLoader hideFromView:self.view animated:YES];
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"会议详情：%@",model);
                meetData = model[@"data"][0];
                [self setUI];
                [self setValue];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            [GMDCircleLoader hideFromView:self.view animated:YES];
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
    
    
}

- (void)setUI{
    WS(ws);
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = NO;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    UIView *container = [UIView new];
    [scrollView addSubview:container];
    container.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];;
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    CGFloat height  = 30*FitSize;
    CGFloat width = KSWidth;
    //标题
    title = [[UILabel alloc]init];
    title.font = [UIFont systemFontOfSize:20];
    [container addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.centerX.equalTo(container);
        make.height.mas_equalTo(30*FitSize);
    }];
    
    UIView *under = [[UIView alloc]init];
    under.backgroundColor = [UIColor whiteColor];
    [container addSubview:under];
    [under mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(title.mas_bottom).offset(25);
    }];
    under.layer.cornerRadius = 10;
    
    UIColor *fontColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.013 alpha:1.000];
    ReleasePeople = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"发布人:"];
    [ReleasePeople hideUnderLine];
    [under addSubview:ReleasePeople];
    [ReleasePeople mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-15);
        make.left.mas_equalTo(25);
        make.height.mas_equalTo(height);
    }];
    
    ReleaseTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"发日期:"];
    [ReleaseTime hideUnderLine];
    [under addSubview:ReleaseTime];
    [ReleaseTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ReleasePeople.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
    }];
    
    meetPlace = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"会议地点:"];
    [meetPlace hideUnderLine];
    [under addSubview:meetPlace];
    [meetPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ReleaseTime.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
        
    }];
    
    
    meetHost = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"会议主持人:"];
    [meetHost hideUnderLine];
    [under addSubview:meetHost];
    [meetHost mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(meetPlace.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
        
    }];
    
    meetTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"会议时间:"];
    [meetTime hideUnderLine];
    [under addSubview:meetTime];
    [meetTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(meetHost.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
        
    }];
    
    ListPersonNames = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"参会人员:"];
    [ListPersonNames hideUnderLine];
    [under addSubview:ListPersonNames];
    [ListPersonNames mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(meetTime.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-25);
        make.height.mas_equalTo(height);
    }];
    UITapGestureRecognizer *tapListPerson = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapListPerson)];
    [ListPersonNames addGestureRecognizer:tapListPerson];
    
    
    UIButton *showPersonList = [UIButton buttonWithType:UIButtonTypeCustom];
    [under addSubview:showPersonList];
    [showPersonList addTarget:self action:@selector(tapListPerson) forControlEvents:UIControlEventTouchUpInside];
    [showPersonList setTitle:@"展开->" forState:UIControlStateNormal];
    showPersonList.titleLabel.font = FONT(10);
    [showPersonList setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [showPersonList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ListPersonNames);
        make.right.mas_equalTo(-3);
    }];

    [under mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ListPersonNames.mas_bottom).offset(15);
    }];
    
    labelcontent = [[UILabel alloc]init];
    labelcontent.text = @"会议纪要";
    labelcontent.textColor = [UIColor colorWithRed:1.000 green:0.168 blue:0.194 alpha:1.000];
    labelcontent.font = FONT(19);
    [container addSubview:labelcontent];
    [labelcontent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.height.mas_equalTo(height);
    }];
    
    under2 = [[UIView alloc]init];
    [container addSubview:under2];
    [under2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelcontent.mas_bottom).offset(10);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(450);
    }];
    under2.layer.cornerRadius = 10;
    under2.backgroundColor = [UIColor whiteColor];

    webViewzqx = [[UIWebView alloc]init];
    [under2 addSubview:webViewzqx];
    webViewzqx.delegate = self;
    webViewzqx.scalesPageToFit = YES;
    [webViewzqx mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(1);
        make.right.mas_equalTo(-1);
        make.bottom.mas_equalTo(-10);
    }];
    webViewzqx.scrollView.bounces = NO;
    
    meetMinutesLabel =[UILabel new];
    meetMinutesLabel.text = @"会议内容";
    meetMinutesLabel.textColor = [UIColor colorWithRed:1.000 green:0.168 blue:0.194 alpha:1.000];
    meetMinutesLabel.font = FONT(19);
    [container addSubview:meetMinutesLabel];
    [meetMinutesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under2.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(30);
        
    }];
    
    UIView *under3 = [[UIView alloc]init];
    [container addSubview:under3];
    [under3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(meetMinutesLabel.mas_bottom).offset(5);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(200);
    }];
    under3.backgroundColor = [UIColor whiteColor];
    under3.layer.cornerRadius = 10;
    
    meetMainTopics = [[UITextView alloc]init];
    meetMainTopics.font = FONT(18);
    [under3 addSubview:meetMainTopics];
    [meetMainTopics mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.equalTo(ReleasePeople);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(180);
    }];

    
    UIView *under4 = [[UIView alloc]init];
    [container addSubview:under4];
    [under4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under3.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(40*FitSize);
    }];
    under4.backgroundColor = [UIColor whiteColor];
    under4.layer.cornerRadius = 10;

    UILabel *attachment = [[UILabel alloc]init];
    [under4 addSubview:attachment];
    [attachment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
        make.centerY.equalTo(under4);
    }];
    attachment.text = @"相关附件:";
    
    [container mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(under4.mas_bottom).offset(30);
    }];
}

//显示整个参会人员列表
- (void)tapListPerson{
    NSMutableArray *personListArray = [NSMutableArray new];
    NSString *allPersonString = meetData[@"ListPersonNames"];
    NSRange range = NSMakeRange(0, 100);
    while (range.location != NSNotFound) {
        range =  [allPersonString rangeOfString:@","];
        if (range.length > 0) {
            NSString *string = [allPersonString substringWithRange:NSMakeRange(0, range.location)];
            allPersonString  = [allPersonString substringWithRange:NSMakeRange(range.location+1, allPersonString.length-range.location-1)];
            [personListArray addObject:string];
        }else{
//            [personListArray addObject:allPersonString];
        
        }
       
    }
    NSLog(@"所有参会人员：%@",personListArray);
    

    AllPersonJoinMeetTableViewController *allPerson = [[AllPersonJoinMeetTableViewController alloc]init];
    allPerson.personList = personListArray;
    [self.navigationController pushViewController:allPerson animated:YES];
    

}

/**
 *  webView开始加载
 */
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [LBProgressHUD showHUDto:webView animated:YES];
}
/**
 *  webView结束加载
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [LBProgressHUD hideAllHUDsForView:webView animated:YES];
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'";
    [webViewzqx stringByEvaluatingJavaScriptFromString:str];
    
}
@end
